package com.dingding.gmall.cart.controller;

import com.dingding.gmall.cart.dto.UserInfoDTO;
import com.dingding.gmall.cart.interceptor.CartInterceptor;
import com.dingding.gmall.cart.service.impl.CartServiceImpl;
import com.dingding.gmall.cart.vo.Cart;
import com.dingding.gmall.cart.vo.CartItem;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

/**
 * @author liudingding
 * @ClassName CartController
 * @description
 * @date 2021/7/29 10:58 下午
 */

@Slf4j
@Controller
public class CartController {

    @Autowired
    private CartServiceImpl cartService;

    @GetMapping("/checked")
    @ResponseBody
    public List<CartItem> getCheckedCartItems(){
        return cartService.getCheckedCartItems();
    }

    @GetMapping("cart/list")
    public String cartListPage(Model model){
        UserInfoDTO userInfoDTO = CartInterceptor.threadLocal.get();
        log.info("userInfo:{}",userInfoDTO);
        Cart cart = cartService.getCart();
        model.addAttribute("cart", cart);
        return "cartList";
    }

    /**
     * 商品添加到购物车
     * @param skuId
     * @param num
     * @return
     */
    @GetMapping("addToCart")
    public String addCartItem(@RequestParam("skuId") Long skuId,
                              @RequestParam("num") Integer num,
                              RedirectAttributes attributes){

        cartService.addToCart(skuId,num);

        attributes.addAttribute("skuId",skuId);
        return "redirect:http://127.0.0.1:40000/add_to_cart_success_page";
    }

    /**
     * 跳转到添加购物车成功页面
     * @param skuId
     * @param model
     * @return
     */
    @GetMapping("/add_to_cart_success_page")
    public String addToCartSuccessPage(@RequestParam("skuId") Long skuId, Model model) {
        UserInfoDTO userInfoDTO = CartInterceptor.threadLocal.get();
        log.info("userInfo:{}",userInfoDTO);
        //重定向到成功页面。再次查询购物车数据即可
        CartItem cartItem = cartService.getCartItem(skuId);
        model.addAttribute("cartItem",cartItem);
        return "success";
    }

    @GetMapping("checkItem")
    public String checkCartItem(@RequestParam("skuId")Long skuId, @RequestParam("check")Integer check) {
        cartService.checkItem(skuId, check);
        return "redirect:http://127.0.0.1:40000/cart/list";
    }

    @GetMapping(value = "/countItem")
    public String countItem(@RequestParam(value = "skuId") Long skuId, @RequestParam(value = "num") Integer num) {
        cartService.changeItemCount(skuId, num);
        return "redirect:http://127.0.0.1:40000/cart/list";
    }

    @GetMapping(value = "/deleteItem")
    public String deleteItem(@RequestParam("skuId") Integer skuId) {
        cartService.deleteIdCartInfo(skuId);
        return "redirect:http://127.0.0.1:40000/cart/list";
    }
}
